package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.cross.CrossGroupMonthRankListModel;
import com.yanqu.road.logic.cross.CrossGroupRankListModel;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossGroupUserMonthAbilityRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

/**
 *
 */
@Cmd(code = CrossProtocol.C_UNION_CROSS_GROUP_RANK, desc = "分组榜单数据")
public class CrossUnionUserGroupRankCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        RankProto.CrossGroupRankReqMsg req = RankProto.CrossGroupRankReqMsg.parseFrom(packet.getBytes());
        CrossGroupMonthRankListModel rankListModel = CrossGroupUserMonthAbilityRankMgr.getCrossGroupRankListModelMap(req.getGroupId(), DateHelper.getMonth(), false);
        RankProto.RankListRespMsg.Builder respMsg = RankProto.RankListRespMsg.newBuilder();
        respMsg.setBigRankType(req.getBigRankType());
        respMsg.setRankType(req.getGameType());

        int myRank = -1;
        RankProto.RankListMsg.Builder rankListMsg = RankProto.RankListMsg.newBuilder();
        if (null != rankListModel) {
            rankListMsg = rankListModel.getRankMsgByPagePara(req.getPageNo(), req.getPageSize());

            CrossUserRank userRank = rankListModel.getMyCrossUserRank(packet.getPlayerId(), serverId);
            if (null != userRank && !StringUtils.isNullOrEmpty(userRank.getParam())){
                myRank = userRank.getRank();
                respMsg.setParam(userRank.getParam());
            }
        }
        respMsg.setRankList(rankListMsg);

        respMsg.setMyRank(myRank);
        if (myRank == -1) {//不在榜上的玩家取成员表里的档位
            CrossUnionMember member = CrossUnionMgr.getUnionMemberByUserId(packet.getPlayerId());
            if (null != member){
                respMsg.setParam(String.valueOf(member.getAbilityRankGrade()));
            } else {
                int lastGrade = ConfigMgr.getLastMonthAbilityRankGrade();
                respMsg.setParam(String.valueOf(lastGrade));
            }
        }

        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_USER_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
